package com.ruoyi.web.controller.app;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.order.domain.app.from.cart.CartFrom;
import com.ruoyi.order.service.ICategoryService;
import com.ruoyi.order.domain.app.Vo.CartResultVo;
import com.ruoyi.order.domain.app.Vo.CartItem;
import com.ruoyi.order.domain.app.Vo.ShopCartItemDiscount;
import com.ruoyi.order.domain.app.Vo.ShopMoneyVo;
import com.ruoyi.order.service.IShoppingCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 陈焕然
 * @version 1.0
 */
@Controller
@RequestMapping("/app")

public class CartAppController extends BaseController {

    @Autowired
    private IShoppingCarService shoppingCarService;


    @RequestMapping("/p/shopCart/prodCount")
    @ResponseBody
    public Integer  prodCount(){
        int count = shoppingCarService.getCount();
        return count;
    }

    @RequestMapping("/p/shopCart/totalPay")
    @ResponseBody
    public ShopMoneyVo  getTotalPay(Integer[] basketId){
        return shoppingCarService.getTotalPay(basketId);
    }

    @RequestMapping("/p/shopCart/info")
    @ResponseBody
    public List<CartResultVo>  getShopCartInfo(Integer[] basketIds){
        List<CartResultVo> result = new ArrayList<>();
        result.add(new CartResultVo());
        if(basketIds!=null&&basketIds.length > 0){
            List<ShopCartItemDiscount> shopCartItemDiscounts = new ArrayList<>();
            ShopCartItemDiscount shopCartItemDiscount = new ShopCartItemDiscount();
            List<CartItem> cartItemList = shoppingCarService.getShopCartProductInfoByBasketIds(basketIds);
            if(cartItemList!=null&&cartItemList.size()>0){
                shopCartItemDiscount.setShopCartItems(cartItemList);
                shopCartItemDiscounts.add(shopCartItemDiscount);
                result.get(0).setShopCartItemDiscounts(shopCartItemDiscounts);
            }
        }else{
            List<ShopCartItemDiscount> shopCartItemDiscounts = new ArrayList<>();
            ShopCartItemDiscount shopCartItemDiscount = new ShopCartItemDiscount();
            List<CartItem> cartItemList = shoppingCarService.getShopCartProductInfo(getUserId().intValue());
            if(cartItemList!=null&&cartItemList.size()>0){
                shopCartItemDiscount.setShopCartItems(cartItemList);
                shopCartItemDiscounts.add(shopCartItemDiscount);
                result.get(0).setShopCartItemDiscounts(shopCartItemDiscounts);
            }
        }
        return result;
    }

    /**
     * 更新购物车
     * @param
     * {"basketId":0,"count":1,"prodId":1255,"shopId":1,"skuId":3211,"distributionCardNo":""}
     * @return
     */
    @RequestMapping("/p/shopCart/changeItem")
    public String  setCartChangeItem(CartFrom cartFrom){
       int result = shoppingCarService.setCartChangeItem(cartFrom,getUserId().intValue());
       return result>0?"添加成功":"添加失败";
    }

    /**
     * 更新购物车
     * @param
     * {"basketId":0,"count":1,"prodId":1255,"shopId":1,"skuId":3211,"distributionCardNo":""}
     * @return
     */
    @RequestMapping("/p/shopCart/deleteItem")
    public void  setCartChangeItem(Integer[] basketIds){
        for (Integer basketId : basketIds) {
            shoppingCarService.deleteShoppingCarById(basketId);
        }
    }
}
